package org.xiaoyu.rpc.remoting.transport;

import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageCodec;
import org.xiaoyu.rpc.remoting.codec.PacketCodeC;
import org.xiaoyu.rpc.remoting.packet.Packet;

import java.util.List;

/**
 * Created By Have
 * 2021/7/5 14:12
 */
@ChannelHandler.Sharable
public class PacketCodecHandler extends MessageToMessageCodec<ByteBuf, Packet> {
    public static final PacketCodecHandler INSTANCE = new PacketCodecHandler();

    @Override
    protected void encode(ChannelHandlerContext ctx, Packet msg, List<Object> out) throws Exception {
        List<ByteBuf> encodes = PacketCodeC.getSingleton().encode(ctx.channel(), msg, out);
        out.addAll(encodes);
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
        Object decode = PacketCodeC.getSingleton().decode(ctx.channel(), msg, out);
        if (decode != null) {
            out.add(decode);
        }
    }

}
